package uabc.arduino.archivo;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;

public class CsvManager {
	private CSVWriter writer;
	private CSVReader reader;

	public CsvManager(String path, String[] encabezado) {
		try {
			if (!path.endsWith(".csv"))
				path = path + ".csv";
			writer = new CSVWriter(new FileWriter(path), ',');
			writer.writeNext(encabezado);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public CsvManager(String path) {
		try {
			if (!(path.endsWith(".csv") || path.endsWith(".CSV")))
				path = path + ".csv";
			reader = new CSVReader(new FileReader(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void escribir(String[] valores) {
		writer.writeNext(valores);
	}

	public ArrayList<String[]> readerInfo() {
		try {
			return (ArrayList<String[]>) reader.readAll();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void cerrar() {
		try {
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
